package states
{
	import levels.Level;
	import levels.Level1;
	import levels.Level2;
	import levels.Level3;
	import org.flixel.*;
	import monsters.Downer;
	import monsters.Lefty;
	import monsters.Upper;
	
	public class PlayState extends FlxState
	{
		private var player:Player;
		private var firstLevel:Level1;
		private var secondLevel:Level2;
		private var thirdLevel:Level3;
		public static var levelAtual:Level;
		public static var levelInt:int = 0;
		private var score:FlxText;
		
		private var starFX:FlxSound;
		
		//Controle de Sprites
		private var controle:int;
		
		//Game timer variables
		private var gameTimer : FlxTimer;
		private var gameTime : Number = 15;
		
		//Text variables
		private var timeText : FlxText;
		
		public function PlayState() 
		{
		}
		
		override public function create():void
		{
			controle = 0;
			
			firstLevel = new Level1;
			levelAtual = firstLevel;
			starFX = new FlxSound();
			starFX.loadEmbedded(starSFX);
			
			player = new Player(8, 240);
			
			score = new FlxText(0, 0, 100);
			score.color = 0xffffffff;
			score.shadow = 0xff000000;
			score.scrollFactor.x = 0;
			score.scrollFactor.y = 0;
			score.text = "0 / " + firstLevel.totalStars.toString();
			
			add(firstLevel.sky);
			add(firstLevel.uppers);
			add(firstLevel.map);
			add(firstLevel.elevator1);
			add(firstLevel.elevator2);
			add(player);
			add(firstLevel.stars);
			add(firstLevel.lefties);
			add(score);
			add(firstLevel.downers);
			
			//	Tell flixel how big our game world is
			FlxG.worldBounds = new FlxRect(0, 0, firstLevel.width, firstLevel.height);
			
			//	Don't let the camera wander off the edges of the map
			FlxG.camera.setBounds(0, 0, firstLevel.width, firstLevel.height);
			
			//	The camera will follow the player
			FlxG.camera.follow(player, FlxCamera.STYLE_PLATFORMER);
			
			FlxG.playMusic(level1MusicMP3, 0.5);
		}
		
		override public function update():void
		{
			super.update();
			
			FlxG.collide(player, levelAtual);
			FlxG.collide(firstLevel.lefties, levelAtual);
			
			FlxG.overlap(player, levelAtual.lefties, hitLefty);
			FlxG.overlap(player, levelAtual.uppers, hitUpper);
			FlxG.overlap(player, levelAtual.stars, hitStar);
			FlxG.overlap(player, levelAtual.downers, hitDowner);
			
			if (FlxG.keys.justPressed("X"))
			{
				player.change(0);
				levelAtual.change(0);
			}
			else if (FlxG.keys.justPressed("C"))
			{
				player.change(1);
				levelAtual.change(1);
			}
			else if (FlxG.keys.justPressed("V"))
			{
				player.change(2);
				levelAtual.change(2);
			}

			
			//	Player walked through end of level exit?
			if (player.x > Registry.levelExit.x && player.y == Registry.levelExit.y)
			{
				segundoNivel();
			}
		}
		
		private function segundoNivel():void
		{
			if (secondLevel == null)
			{
				remove(firstLevel.sky);
				remove(firstLevel.uppers);
				remove(firstLevel.map);
				remove(firstLevel.elevator1);
				remove(firstLevel.elevator2);
				remove(player);
				remove(firstLevel.stars);
				remove(firstLevel.lefties);
				remove(score);
				remove(firstLevel.downers);
				
				controle = 0;
				levelInt = 1;
				secondLevel = new Level2();
				levelAtual = secondLevel;
				
				player = new Player(0, 340);
				
				score = new FlxText(0, 0, 100);
				score.color = 0xffffffff;
				score.shadow = 0xff000000;
				score.scrollFactor.x = 0;
				score.scrollFactor.y = 0;
				score.text = "0 / " + secondLevel.totalStars.toString();
				
				add(secondLevel.sky);
				add(secondLevel.uppers);
				add(secondLevel.map);
				add(secondLevel.elevator1);
				add(secondLevel.elevator2);
				add(player);
				add(secondLevel.stars);
				add(secondLevel.lefties);
				add(score);
				add(secondLevel.downers);
				
				//	Tell flixel how big our game world is
				FlxG.worldBounds = new FlxRect(0, 0, secondLevel.width, secondLevel.height);
				
				//	Don't let the camera wander off the edges of the map
				FlxG.camera.setBounds(0, 0, secondLevel.width, secondLevel.height);
				
				//	The camera will follow the player
				FlxG.camera.follow(player, FlxCamera.STYLE_PLATFORMER);
				
			}else
			{
				terceiroNivel();
			}
		}
		
		private function terceiroNivel():void
		{
			if (thirdLevel == null)
			{
				remove(secondLevel.sky);
				remove(secondLevel.uppers);
				remove(secondLevel.map);
				remove(secondLevel.elevator1);
				remove(secondLevel.elevator2);
				remove(player);
				remove(secondLevel.stars);
				remove(secondLevel.lefties);
				remove(score);
				remove(secondLevel.downers);
				
				controle = 0;
				
				levelInt = 2;
				thirdLevel = new Level3();
				levelAtual = thirdLevel;
				score = new FlxText(0, 0, 100);
				score.color = 0xffffffff;
				score.shadow = 0xff000000;
				score.scrollFactor.x = 0;
				score.scrollFactor.y = 0;
				score.text = "0 / " + thirdLevel.totalStars.toString();
				
				add(thirdLevel.sky);
				add(thirdLevel.uppers);
				add(thirdLevel.map);
				add(thirdLevel.elevator1);
				add(thirdLevel.elevator2);
				add(player);
				add(thirdLevel.stars);
				add(thirdLevel.lefties);
				add(score);
				add(thirdLevel.downers);
				
				//	Tell flixel how big our game world is
				FlxG.worldBounds = new FlxRect(0, 0, thirdLevel.width, thirdLevel.height);
				
				//	Don't let the camera wander off the edges of the map
				FlxG.camera.setBounds(0, 0, thirdLevel.width, thirdLevel.height);
				
				//	The camera will follow the player
				FlxG.camera.follow(player, FlxCamera.STYLE_PLATFORMER);
				
			}else
			{
				player.exists = false;
				FlxG.fade(0xff000000, 2, changeState);
				FlxG.music.fadeOut(2);
			}
		}
		
		private function changeState():void
		{
			FlxG.switchState(new LevelEndState);
		}
		
		private function hitLefty(player:FlxObject, lefty:FlxObject):void
		{
			if (Lefty(lefty).isDying)
			{
				return;
			}
			
			if (player.y < lefty.y)
			{
				lefty.kill();
				Player(player).jump();
			}
			else
			{
				Player(player).restart();
			}
		}		
		
		
		private function hitUpper(player:FlxObject, upper:FlxObject):void
		{
			if (Upper(upper).isDying)
			{
				return;
			}
			else
			{
				Player(player).restart();
			}
		}
		
		private function hitDowner(player:FlxObject, downer:FlxObject):void
		{
			if (Downer(downer).isDying)
			{
				return;
			}
			
			if (player.y < downer.y)
			{				
				downer.kill();
				Player(player).jump();
			}
			else
			{
				Player(player).restart();
			}
		}
		
		private function hitStar(p:FlxObject, star:FlxObject):void
		{
			star.kill();
			
			FlxG.score += 1;
			
			starFX.play(true);
			
			firstLevel.openExit();
			
			if (FlxG.score == firstLevel.totalStars)
			{
				//	Opens the exit at the end of the level
				score.text = FlxG.score.toString() + " / " + firstLevel.totalStars.toString() + " EXIT OPEN!";
				levelAtual.openExit();
			}
			else
			{
				score.text = FlxG.score.toString() + " / " + firstLevel.totalStars.toString();
			}
		}
		
	}

}